package main.controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyEvent;
import main.dao.BookTableDB;
import main.dao.HistoryDB;
import main.model.Book;
import main.view.Main;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BorrowBook {

    private String value;

    public static Book momentBook;

    @FXML
    private TextField secrchbox;

    @FXML
    private Button serach;

    @FXML
    private Button setbacks;

    @FXML
    private Button borrow;

    @FXML
    private TableView<Book> bookTable;

    @FXML
    private TableColumn<Book, String> bookname;

    @FXML
    private TableColumn<Book, String> bookmessage;

    @FXML
    private TableColumn<Book, String> bookamount;

    @FXML
    void borrowBook(ActionEvent event) throws SQLException {
        new HistoryDB().writeDate(this.value,Login.momentUser.getUsername());
    }

    @FXML
    void setbacks(ActionEvent event) throws IOException {
        new Main().initUserMenu();
    }

    @FXML
    void inputsearch(KeyEvent event) {

    }

    @FXML
    void searchBook(ActionEvent event) throws SQLException {
        this.value = secrchbox.getText();
        bookname.setCellValueFactory(new PropertyValueFactory<>("title"));
        bookmessage.setCellValueFactory(new PropertyValueFactory<>("message"));
        bookamount.setCellValueFactory(new PropertyValueFactory<>("amount"));
        ResultSet rst =  new BookTableDB().date(this.value);
        ObservableList<Book> data = FXCollections.observableArrayList();
        while(rst.next()) {
            Button edit = new Button("操作");
            Book temp = new Book(rst.getString(1),rst.getString(2),rst.getString(3),rst.getString(4),rst.getString(5),rst.getString(6));
            BorrowBook.momentBook = temp;
            data.add(new Book(rst.getString(1),rst.getString(6),rst.getString(5)));
            bookTable.setItems(data);
        }
    }

}
